package cn.sjx.auth.controller;

import cn.sjx.auth.service.LoginService;
import cn.sjx.basic.utils.AjaxResult;
import cn.sjx.basic.utils.BasicMap;
import cn.sjx.dto.LoginDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class LoginController {

    @Resource
    LoginService loginService;
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDTO loginDTO) {
        try {
            Map<String,Object> map = loginService.login(loginDTO);
            return AjaxResult.me().setData(map);
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("登陆失败"+e.getMessage());
        }
    }

    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            //从BasicMap中删除token键值对
            BasicMap.loginMap.remove(token);
            return AjaxResult.me();
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("退出登陆失败"+e.getMessage());
        }
    }
}
